package com.cxh.rmsq.utils;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;

import java.util.List;
import com.cxh.rmsq.R;
import com.hjq.permissions.OnPermissionPageCallback;
import com.hjq.permissions.XXPermissions;

public final class CustomPermissionUtil {

    //权限使用说明
    public static void showPermissionInfo(Activity activity, Dialog dialog, String title, String content) {
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        View dialogView = LayoutInflater.from(activity).inflate(R.layout.dialog_permission_info, null);
        TextView tv_permission_info_title = dialogView.findViewById(R.id.tv_permission_info_title);
        TextView tv_permission_info_content = dialogView.findViewById(R.id.tv_permission_info_content);
        tv_permission_info_title.setText(title);
        tv_permission_info_content.setText(content);

        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable());
        window.setDimAmount(0.2f);
        window.setGravity(Gravity.TOP);
        // 把 DecorView 的默认 padding 取消，同时 DecorView 的默认大小也会取消
        window.getDecorView().setPadding(ConvertUtils.dp2px(2), ConvertUtils.dp2px(2),
                ConvertUtils.dp2px(2), ConvertUtils.dp2px(2));
        // 设置宽度
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(layoutParams);
        window.setContentView(dialogView);
    }

    //权限设置
    public static void showPermissionSet(Activity activity, String content, List<String> permissions, OnPermissionPageCallback callback) {
        Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        View dialogView = LayoutInflater.from(activity).inflate(R.layout.dialog_permission_set, null);
        TextView tv_permission_set_content = dialogView.findViewById(R.id.tv_permission_set_content);
        TextView tv_permission_set_cancel = dialogView.findViewById(R.id.tv_permission_set_cancel);
        TextView tv_permission_set_sure = dialogView.findViewById(R.id.tv_permission_set_sure);

        tv_permission_set_content.setText(content);
        tv_permission_set_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callback.onDenied();
                activity.finish();
                dialog.dismiss();
            }
        });
        tv_permission_set_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                XXPermissions.startPermissionActivity(activity, permissions, callback);

                dialog.dismiss();
            }
        });

        Window window = dialog.getWindow();
        window.setBackgroundDrawable(new ColorDrawable());
        window.setDimAmount(0.2f);
        window.setGravity(Gravity.BOTTOM);
        // 把 DecorView 的默认 padding 取消，同时 DecorView 的默认大小也会取消
        window.getDecorView().setPadding(ConvertUtils.dp2px(10), ConvertUtils.dp2px(20),
                ConvertUtils.dp2px(10), ConvertUtils.dp2px(20));
        // 设置宽度
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(layoutParams);
        window.getAttributes().windowAnimations = 0;
        window.setContentView(dialogView);
    }
}
